using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using ENTITIES;

namespace DAL
{
  public  class NhanVienDAL
    {
       private SqlConnection cn = null;
        public NhanVienDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
       public void Insert (TBNhanVien NV)
        {
            SqlCommand cm = new SqlCommand("NhanVien_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenNV",NV.tennv);
            cm.Parameters.AddWithValue("@SoDT", NV.sodt);
            cm.Parameters.AddWithValue("@DiaChi", NV.diachi);
            cm.Parameters.AddWithValue("@Email", NV.email);
            cm.Parameters.AddWithValue("@TaiKhoan", NV.taikhoan);
            cm.Parameters.AddWithValue("@MatKhau", NV.matkhau);
            cm.ExecuteNonQuery();
        }
        public void Update(TBNhanVien NV)
        {
            SqlCommand cm = new SqlCommand("nhanvien_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaNV", NV.manv);
            cm.Parameters.AddWithValue("@TenNV", NV.tennv);
            cm.Parameters.AddWithValue("@SoDT", NV.sodt);
            cm.Parameters.AddWithValue("@DiaChi", NV.diachi);
            cm.Parameters.AddWithValue("@Email", NV.email);
            cm.Parameters.AddWithValue("@TaiKhoan", NV.taikhoan);
            cm.Parameters.AddWithValue("@MatKhau", NV.matkhau);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("nhanvien_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaNV", iD);
            cm.ExecuteNonQuery();
        }

        public tbNhanVienCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("nhanvien_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbNhanVienCollection nhanvienCollection = new tbNhanVienCollection();

            while (dr.Read())
            {
                TBNhanVien nhanvien = new TBNhanVien();
                nhanvien.manv = int.Parse(dr.GetValue(0).ToString());
                nhanvien.tennv = dr.GetValue(1).ToString();
                nhanvien.sodt = dr.GetValue(2).ToString();
                nhanvien.diachi = dr.GetValue(3).ToString();
                nhanvien.email = dr.GetValue(4).ToString();
                nhanvien.taikhoan = dr.GetValue(5).ToString();
                nhanvien.matkhau = dr.GetValue(6).ToString();
                nhanvienCollection.Add(nhanvien);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return nhanvienCollection;
        }
    }
    }
